import uniWrapper from "../uniWrapper";

const state = {
    // 是否放大组件, 特指图像显示区域
    showBigImgArea: false,

    // 当前的语言系统
    nowLanguage: 'CHI',

    // 有些场景需要隐藏部分pannel
    segPanelStatus: {
        showRightPanel: false
    },

    // 如果uni-app跨平台失效，自己写
    sidebar: {
        opened: true,
        withoutAnimation: false
    },

    // 从窗体信息推测得到
    device: 'desktop',
    size: 'medium',

    // 此结构体主要用于后续的组建计算
    // 比如计算右侧按钮位置为50%
    winSize: {
        height: 1080,
        width: 1920
    },

    // 用于存储uni-app代理的信息
    uniLeftMenuState: {
        matchLeftWindow: false,
        showLeftWindow: false
    }
}

const mutations = {
    TOGGLE_SIDEBAR: state => {
        state.sidebar.opened = !state.sidebar.opened
        state.sidebar.withoutAnimation = false
        if (state.sidebar.opened) {
            uniWrapper.setUniCache('sidebarStatus', 1)
        } else {
            uniWrapper.setUniCache('sidebarStatus', 0)
        }
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
        uniWrapper.setUniCache('sidebarStatus', 0)
        state.sidebar.opened = false
        state.sidebar.withoutAnimation = withoutAnimation
    },
    TOGGLE_DEVICE: (state, device) => {
        state.device = device
    },
    SET_SIZE: (state, size) => {
        state.size = size
        uniWrapper.setUniCache('size', size)
    },
    SET_WIN_SIZE: (state, size) => {
        state.winSize = size
    },
    SET_UNI_LEFT_MENU_STATE: (state, uniState) => {
        state.uniLeftMenuState = uniState
    },
    SET_PANEL_STATUS: (state, panelState) => {
        state.segPanelStatus = panelState
    },
    SET_SHOW_BIG_IMG_AREA: (state, isBig) => {
        state.showBigImgArea = isBig
    },
    SET_NOW_LANGUAGE: (state, lang) => {
        state.nowLanguage = lang
        uniWrapper.setUniCache('nowLanguage', lang)
    }
}

const actions = {
    toggleSideBar({ commit }) {
        commit('TOGGLE_SIDEBAR')
    },
    closeSideBar({ commit }, { withoutAnimation }) {
        commit('CLOSE_SIDEBAR', withoutAnimation)
    },
    toggleDevice({ commit }, device) {
        commit('TOGGLE_DEVICE', device)
    },
    setSize({ commit }, size) {
        commit('SET_SIZE', size)
    },
    setWinSize({ commit }, size) {
        commit('SET_WIN_SIZE', size)
    },
    setUniLeftMenuState({ commit }, uniState){
        commit('SET_UNI_LEFT_MENU_STATE', uniState)
    },
    setSegPanelState({ commit }, panelState){
        commit('SET_PANEL_STATUS', panelState)
    },
    setShowBigImgArea({ commit }, isBig){
        commit('SET_SHOW_BIG_IMG_AREA', isBig)
    },
    setNowLanguage( { commit }, lang){
        commit('SET_NOW_LANGUAGE', lang)
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
